/*
 * MBTIView.java
 */

package mbti;

import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * The application's main frame.
 */
public class MBTIView extends FrameView {

    JFrame jframe;
    MBTIQuestion currentQuestion;
    int progressMax;
    int progressIndex;
    
    public MBTIView(SingleFrameApplication app) {
        super(app);
        
        initComponents();
        
        this.jframe = this.getFrame();
        this.jframe.setResizable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panQuestion = new javax.swing.JPanel();
        labStep = new javax.swing.JLabel();
        labQuestion = new javax.swing.JLabel();
        rbtnSelectA = new javax.swing.JRadioButton();
        rbtnSelectB = new javax.swing.JRadioButton();
        labProgress = new javax.swing.JLabel();
        prbProgress = new javax.swing.JProgressBar();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        buttonGroup1 = new javax.swing.ButtonGroup();
        panMain = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        btnStart = new javax.swing.JButton();
        panResult = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txaStatement = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();

        panQuestion.setAutoscrolls(true);
        panQuestion.setMinimumSize(new java.awt.Dimension(500, 260));
        panQuestion.setName("panQuestion"); // NOI18N
        panQuestion.setPreferredSize(new java.awt.Dimension(500, 260));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mbti.MBTIApp.class).getContext().getResourceMap(MBTIView.class);
        labStep.setFont(resourceMap.getFont("labStep.font")); // NOI18N
        labStep.setText(resourceMap.getString("labStep.text")); // NOI18N
        labStep.setName("labStep"); // NOI18N

        labQuestion.setFont(resourceMap.getFont("labStep.font")); // NOI18N
        labQuestion.setText(resourceMap.getString("labQuestion.text")); // NOI18N
        labQuestion.setName("labQuestion"); // NOI18N

        buttonGroup1.add(rbtnSelectA);
        rbtnSelectA.setFont(resourceMap.getFont("rbtnSelectA.font")); // NOI18N
        rbtnSelectA.setText(resourceMap.getString("rbtnSelectA.text")); // NOI18N
        rbtnSelectA.setName("rbtnSelectA"); // NOI18N
        rbtnSelectA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Selected(evt);
            }
        });

        buttonGroup1.add(rbtnSelectB);
        rbtnSelectB.setFont(resourceMap.getFont("rbtnSelectB.font")); // NOI18N
        rbtnSelectB.setText(resourceMap.getString("rbtnSelectB.text")); // NOI18N
        rbtnSelectB.setName("rbtnSelectB"); // NOI18N
        rbtnSelectB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Selected(evt);
            }
        });

        labProgress.setText(resourceMap.getString("labProgress.text")); // NOI18N
        labProgress.setName("labProgress"); // NOI18N

        prbProgress.setName("prbProgress"); // NOI18N

        filler1.setName("filler1"); // NOI18N

        javax.swing.GroupLayout panQuestionLayout = new javax.swing.GroupLayout(panQuestion);
        panQuestion.setLayout(panQuestionLayout);
        panQuestionLayout.setHorizontalGroup(
            panQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panQuestionLayout.createSequentialGroup()
                .addGroup(panQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(panQuestionLayout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addGroup(panQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rbtnSelectB, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
                            .addComponent(rbtnSelectA, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE))
                        .addGap(10, 10, 10))
                    .addGroup(panQuestionLayout.createSequentialGroup()
                        .addGroup(panQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panQuestionLayout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addComponent(labStep, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panQuestionLayout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addComponent(labQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE))
                            .addGroup(panQuestionLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(labProgress)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(prbProgress, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        panQuestionLayout.setVerticalGroup(
            panQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panQuestionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panQuestionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(filler1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panQuestionLayout.createSequentialGroup()
                        .addComponent(labStep)
                        .addGap(28, 28, 28)
                        .addComponent(labQuestion)
                        .addGap(18, 18, 18)
                        .addComponent(rbtnSelectA)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rbtnSelectB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(labProgress)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(prbProgress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panMain.setMinimumSize(new java.awt.Dimension(800, 400));
        panMain.setName("panMain"); // NOI18N
        panMain.setPreferredSize(new java.awt.Dimension(800, 400));

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setVerifyInputWhenFocusTarget(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setBackground(resourceMap.getColor("jTextArea1.background")); // NOI18N
        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(resourceMap.getFont("jTextArea1.font")); // NOI18N
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setText("1、参加测试的人员请务必诚实、独立地回答问题，只有如此，才能得到有效的结果。 \n2、《性格分析报告》展示的是你的性格倾向，而不是你的知识、技能、经验。 \n3、MBTI提供的性格类型描述仅供测试者确定自己的性格类型之用，性格类型没有好坏，只有不同。每一种性格特征都有其价值和优点，也有缺点和需要注意的地方。清楚地了解自己的性格优劣势，有利于更好地发挥自己的特长，而尽可能的在为人处事中避免自己性格中的劣势，更好地和他人相处，更好地作重要的决策。 \n4、本软件提供的MBTI-M量表题目数量为93题，时间大约为20分钟。所有题目没有对错之分，请根据自己的实际情况选择。 \n\n*只要你是认真、真实地填写了测试问卷，那么通常情况下你都能得到一个确实和你的性格相匹配的类型。希望你能从中或多或少地获得一些有益的信息。\n"); // NOI18N
        jTextArea1.setDoubleBuffered(true);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        btnStart.setFont(resourceMap.getFont("btnStart.font")); // NOI18N
        btnStart.setText(resourceMap.getString("btnStart.text")); // NOI18N
        btnStart.setName("btnStart"); // NOI18N
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panMainLayout = new javax.swing.GroupLayout(panMain);
        panMain.setLayout(panMainLayout);
        panMainLayout.setHorizontalGroup(
            panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panMainLayout.createSequentialGroup()
                .addGroup(panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panMainLayout.createSequentialGroup()
                        .addGap(332, 332, 332)
                        .addGroup(panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel1)
                            .addComponent(btnStart)))
                    .addGroup(panMainLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)))
                .addGap(10, 10, 10))
        );
        panMainLayout.setVerticalGroup(
            panMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panMainLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btnStart)
                .addGap(10, 10, 10))
        );

        panResult.setMinimumSize(new java.awt.Dimension(800, 600));
        panResult.setName("panResult"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        txaStatement.setBackground(resourceMap.getColor("txaStatement.background")); // NOI18N
        txaStatement.setColumns(20);
        txaStatement.setEditable(false);
        txaStatement.setFont(resourceMap.getFont("txaStatement.font")); // NOI18N
        txaStatement.setLineWrap(true);
        txaStatement.setRows(5);
        txaStatement.setDoubleBuffered(true);
        txaStatement.setName("txaStatement"); // NOI18N
        jScrollPane2.setViewportView(txaStatement);

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setVerifyInputWhenFocusTarget(false);

        btnClose.setFont(resourceMap.getFont("btnClose.font")); // NOI18N
        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panResultLayout = new javax.swing.GroupLayout(panResult);
        panResult.setLayout(panResultLayout);
        panResultLayout.setHorizontalGroup(
            panResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panResultLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addComponent(btnClose))
                .addContainerGap())
        );
        panResultLayout.setVerticalGroup(
            panResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panResultLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btnClose)
                .addContainerGap())
        );

        jLabel2.getAccessibleContext().setAccessibleName(resourceMap.getString("jLabel2.AccessibleContext.accessibleName")); // NOI18N

        setComponent(panMain);
    }// </editor-fold>//GEN-END:initComponents
  
    /** 选择一个选项后做的处理
     */
    private void Selected(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Selected
        // TODO add your handling code here:
        Object source = evt.getSource();
        
       
        if(source == rbtnSelectA)
        {
            MBTIApp.MBTIRuslt.Add(currentQuestion.getSelectAParameter());
        }
        else if(source == rbtnSelectB)
        {
            MBTIApp.MBTIRuslt.Add(currentQuestion.getSelectBParameter());
        }

        MBTIApp.questionIndex ++;
        if(MBTIApp.stepIndex < MBTIApp.StepAll.length)
        {
            if(MBTIApp.questionIndex >= MBTIApp.StepAll[MBTIApp.stepIndex].length)
            {
                MBTIApp.questionIndex = 0;
                MBTIApp.stepIndex ++;
            }
            
            if(MBTIApp.stepIndex < MBTIApp.StepAll.length)
            {
                LoadNewQuestion();
            }
            else
            {
                ShowStatement();
            }
        }
        else
        {
            JOptionPane.showMessageDialog(this.getComponent(), "Sorry~ 出错了！");
        }
    }//GEN-LAST:event_Selected

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        // TODO add your handling code here:
        StartTest();  
    }//GEN-LAST:event_btnStartActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        System.exit(1);
    }//GEN-LAST:event_btnCloseActionPerformed
  
    /*
     * 开始测试
     */
    private void StartTest(){
        
        MBTIApp.InitMBTI();
        progressMax = 93;
        progressIndex = 0;
        prbProgress.setMinimum(0);
        prbProgress.setMaximum(progressMax);
        LoadNewQuestion();
        
        
        this.jframe.getContentPane().removeAll();
        this.jframe.getContentPane().add(panQuestion);//JPanel换成自己的panel
        this.jframe.getContentPane().validate();
        
        this.jframe.getContentPane().repaint();
        this.jframe.setSize(500, 300);
        this.jframe.setLocationRelativeTo(null);
    }
    /*
     * 载入下一问题
     */
    private void LoadNewQuestion(){        
        currentQuestion =MBTIApp.StepAll[MBTIApp.stepIndex][MBTIApp.questionIndex];
        if(currentQuestion != null){
            if(MBTIApp.questionIndex == 0)
            {
                labStep.setText("<html>" + MBTIApp.StepStrings[MBTIApp.stepIndex] + "</html>");
            }
            labQuestion.setText("<html>" + currentQuestion.getQuestionString() + "</html>");
            rbtnSelectA.setText("<html>" + currentQuestion.getSelectAString() + "</html>");
            rbtnSelectB.setText("<html>" + currentQuestion.getSelectBString() + "</html>");
            
            progressIndex++;
            prbProgress.setValue(progressIndex);
            labProgress.setText(progressIndex + "/" + progressMax);
        }  
    }
    /*
     * 显示测试报告
     */
    private void ShowStatement(){
        
        MBTITypes type = MBTIApp.MBTIRuslt.getType();

        txaStatement.append("您的类型是："+type.toString() + "\n\n");

        txaStatement.append("【特征】\n");
        txaStatement.append(MBTIStatements.getCharacteristics(type) + "\n\n");
        
        txaStatement.append("【适合领域】\n");
        txaStatement.append(MBTIStatements.getAreas(type) + "\n\n");
        
        txaStatement.append("【适合职业】\n");
        txaStatement.append(MBTIStatements.getJobs(type) + "\n\n");
        
        txaStatement.append("【详细描述】\n");
        txaStatement.append(MBTIStatements.getDescribes(type) + "\n\n");
        
        this.jframe.getContentPane().removeAll();
        this.jframe.getContentPane().add(panResult);//JPanel换成自己的panel
        this.jframe.getContentPane().validate();

        this.jframe.getContentPane().repaint();
        this.jframe.setSize(800,600);
        this.jframe.setLocationRelativeTo(null); 
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnStart;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel labProgress;
    private javax.swing.JLabel labQuestion;
    private javax.swing.JLabel labStep;
    private javax.swing.JPanel panMain;
    private javax.swing.JPanel panQuestion;
    private javax.swing.JPanel panResult;
    private javax.swing.JProgressBar prbProgress;
    private javax.swing.JRadioButton rbtnSelectA;
    private javax.swing.JRadioButton rbtnSelectB;
    private javax.swing.JTextArea txaStatement;
    // End of variables declaration//GEN-END:variables

}
